package io.xxx.xbutler.core.event.job;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.xxx.xbutler.data.TaskMapper;
import io.xxx.xbutler.data.TaskTemplateItemMapper;
import io.xxx.xbutler.data.TaskTemplateMapper;
import io.xxx.xbutler.domain.User;
import io.xxx.xbutler.domain.task.Task;
import io.xxx.xbutler.domain.task.TaskItem;
import io.xxx.xbutler.domain.task.TaskTemplate;
import io.xxx.xbutler.domain.task.TaskTemplateItem;
import io.xxx.xbutler.service.TaskService;
import jakarta.annotation.Nonnull;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.tuple.Pair;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.JobExecutionContext;
import org.springframework.beans.BeanUtils;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Component
@DisallowConcurrentExecution
public class CreateTaskJob extends QuartzJobBean {

    @Resource
    private TaskService taskService;

    @Resource
    private TaskTemplateMapper templateMapper;

    @Resource
    private TaskTemplateItemMapper itemMapper;

    @Resource
    private TaskMapper taskMapper;

    @Override
    protected void executeInternal(@Nonnull JobExecutionContext context) {
        log.info("创建任务开始执行");

        LocalDateTime now = LocalDateTime.now();
        List<TaskTemplate> templates = templateMapper.findTaskTemplates(now);
        for (TaskTemplate template : templates) {
            Long templateId = template.getId();
            LambdaQueryWrapper<Task> wrapper = new LambdaQueryWrapper<Task>()
                    .eq(Task::getTemplateId, templateId)
                    .eq(Task::getStartTime, template.getStartTime());
            boolean exists = taskMapper.exists(wrapper);
            if (!exists) {
                Pair<Task, List<TaskItem>> pair = build(template, now);
                Task task = pair.getLeft();
                List<TaskItem> items = pair.getRight();
                taskService.create(task, items, new User(-1L, "系统任务"));
                log.info("任务已创建[taskId:{}, templateId:{}]", task.getId(), templateId);
            }
        }
    }

    private Pair<Task, List<TaskItem>> build(TaskTemplate template, LocalDateTime now) {
        Long templateId = template.getId();

        Task task = new Task();
        BeanUtils.copyProperties(template, task);
        task.setTemplateId(templateId);
        task.setCreatedTime(now);

        LambdaQueryWrapper<TaskTemplateItem> wrapper = new LambdaQueryWrapper<TaskTemplateItem>()
                .eq(TaskTemplateItem::getTemplateId, templateId);
        List<TaskItem> items = itemMapper.selectList(wrapper)
                .stream()
                .map(it -> {
                    TaskItem item = new TaskItem();
                    BeanUtils.copyProperties(it, item);
                    return item;
                })
                .toList();

        return Pair.of(task, items);
    }
}
